package com.gvfish.other;
import java.util.Locale;   
import java.util.Map;   
  
import org.springframework.web.servlet.View;   
import org.springframework.web.servlet.ViewResolver;   
import org.springframework.web.servlet.view.UrlBasedViewResolver;   
  
public class MultipleViewResolver extends UrlBasedViewResolver {   
  
    private Map<String, ViewResolver> resolvers;   
       
    @Override   
    protected View loadView(String viewName, Locale locale) throws Exception {   
        // TODO Auto-generated method stub   
        return internalResolveViewName(viewName, locale);   
    }   
  
    public View internalResolveViewName(String viewName, Locale locale)   
            throws Exception {   
        // �����չ��   
        int n = viewName.lastIndexOf(".");   
//        if (n == -1)   
//            return null;   
//        String suffix = viewName.substring(n + 1);   
        String suffix="";
        if(n!=-1){
        	suffix=viewName.substring(n+1);
        }else{
        	suffix="json";
        }
  
        String resolverViewKey = "";   
        // ���� eg. zftl,ftl = oneResolver => ftl = oneResolver   
        for (String key : resolvers.keySet()) {  
        	
            String[] arr = key.split(",");   
            for (String subViewName : arr) {   
                if (subViewName.equals(suffix)) {   
                    resolverViewKey = key;   
                    break;   
                }   
            }   
        }   
  
        // ȡ����Ӧ��ViewResolver:   
        ViewResolver resolver = resolvers.get(resolverViewKey);   
        if (resolver != null)   
            return resolver.resolveViewName(viewName, locale);   
  
        // û���ҵ���Ӧ��ViewResolver�����쳣   
        return null;   
    }   
  
    public Map<String, ViewResolver> getResolvers() {   
        return resolvers;   
    }   
  
    public void setResolvers(Map<String, ViewResolver> resolvers) {   
        this.resolvers = resolvers;   
    }   
  
} 